//
//  FPLoginViewController.m
//  FilmParadise
//
//  Created by lanou3g on 15/7/23.
//  Copyright (c) 2015年 DLYgroup. All rights reserved.
//

#import "FPLoginViewController.h"
#import "FPLoginView.h"
#import "UMSocial.h"
#import "FPSqliteManager.h"
#import "FPUserModel.h"
#import "UMSocialQQHandler.h"
#import "FPRegisterViewController.h"
@interface FPLoginViewController ()<UITextFieldDelegate>
@property(nonatomic,strong)FPLoginView *loginView;

@end

@implementation FPLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"cgClear"] forBarMetrics:UIBarMetricsDefault];
    UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth , 20)];
    statusBarView.backgroundColor = [UIColor colorWithRed:0.180 green:0.596 blue:1.000 alpha:1.000];
    [self.view addSubview:statusBarView];

    self.loginView = [[FPLoginView alloc]initWithFrame:CGRectMake(0, 64 , kScreenWidth, kScreenHeight)];
    self.loginView.numTextFiled.delegate =self;
    self.loginView.pswTextFiled.delegate = self;
    [self.loginView.loginButton addTarget:self action:@selector(loginButtonAction:) forControlEvents:UIControlEventTouchUpInside];
   [self.loginView.registerBtn addTarget:self action:@selector(registerBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.loginView.weiboBtn addTarget:self action:@selector(weiboBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.loginView.QQbtn addTarget:self action:@selector(QQbtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_loginView];
}
- (void)loginButtonAction:(UIButton *)sender
{
    NSLog(@"登录");
    
    NSArray *nameArray = [FPSqliteManager selectAllUserName];
    NSArray *pwdArray = [FPSqliteManager selectAllUserPWD];
    if ([nameArray containsObject:self.loginView.numTextFiled.text])
    {
        NSInteger index =  [nameArray indexOfObject:self.loginView.numTextFiled.text];
        if ([pwdArray[index] isEqualToString:self.loginView.pswTextFiled.text]) {
            [self showAlert:@"登录成功"];
            [FPUserModel ShareInstance].userName = self.loginView.numTextFiled.text;
            [FPUserModel ShareInstance].userPwd = self.loginView.pswTextFiled.text;
            [self.navigationController popViewControllerAnimated:YES];

        }
        else
        {
            [self showAlert:@"密码错误"];
        }
    }
    else
    {
        [self showAlert:@"无效的用户名"];
    }
}

- (void)registerBtnAction:(UIButton *)sender
{
     NSLog(@"免费注册");
    FPRegisterViewController *regsiterVc = [[FPRegisterViewController alloc]init];
    [self.navigationController pushViewController:regsiterVc animated:YES];
    
}

- (void)weiboBtnAction:(UIButton *)sender
{
     NSLog(@"微博登录");
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        // 获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            [FPUserModel ShareInstance].userName = snsAccount.userName;
        
            NSURL *url = [NSURL URLWithString:snsAccount.iconURL];
            NSData *data = [NSData dataWithContentsOfURL:url];
            [FPUserModel ShareInstance].userImage = [UIImage imageWithData:data];
            [FPUserModel ShareInstance].userPwd = @"111";
            [FPSqliteManager insertUserModel:[FPUserModel ShareInstance]];
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }});

   

}

- (void)QQbtnAction:(UIButton *)sender
{
    NSLog(@"qq登录");
    UMSocialSnsPlatform *snsPlatform  = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToQQ];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            [FPUserModel ShareInstance].userName = snsAccount.userName;
            NSURL *url = [NSURL URLWithString:snsAccount.iconURL];
            NSData *data = [NSData dataWithContentsOfURL:url];
            [FPUserModel ShareInstance].userImage = [UIImage imageWithData:data];
            [FPUserModel ShareInstance].userPwd = @"111";
            [FPSqliteManager insertUserModel:[FPUserModel ShareInstance]];
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }});
    [UMSocialConfig hiddenNotInstallPlatforms:@[UMShareToQQ,UMShareToQzone,UMShareToWechatTimeline,UMShareToWechatTimeline]];
    
    
    
    
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return [textField resignFirstResponder];
}
//根据NSTimer来给AlertView定时, 让显示完后消失
- (void)timerFireMethod:(NSTimer*)theTimer//弹出框
{
    UIAlertView *promptAlert = (UIAlertView*)[theTimer userInfo];
    [promptAlert dismissWithClickedButtonIndex:0 animated:NO];
    promptAlert =NULL;
    
}

//弹出AlertView
- (void)showAlert:(NSString *) _message{//时间
    UIAlertView *promptAlert = [[UIAlertView alloc] initWithTitle:@"提示:" message:_message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    
    [NSTimer scheduledTimerWithTimeInterval:1.0f
                                     target:self
                                   selector:@selector(timerFireMethod:)
                                   userInfo:promptAlert
                                    repeats:YES];
    [promptAlert show];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
